package au.com.iglootech.TaxTimeToolTip.ui.fillupform;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import au.com.iglootech.TaxTimeToolTip.Entity.Company;
import au.com.iglootech.TaxTimeToolTip.Entity.Task;
import au.com.iglootech.TaxTimeToolTip.resource.ConstantResource;
import au.com.iglootech.TaxTimeToolTip.service.CompanyService;
import au.com.iglootech.TaxTimeToolTip.service.TaskService;
import au.com.iglootech.TaxTimeToolTip.service.TaxTimeSheetLogService;
import au.com.iglootech.TaxTimeToolTip.ui.company.CompanyFieldsFactory;
import au.com.iglootech.TaxTimeToolTip.ui.company.CreateCompanyTab;
import au.com.iglootech.TaxTimeToolTip.ui.company.ManageCompanyTab;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
@Configurable(preConstruction=true)
@Component
@Scope("request")
public class FillUpTimeSheetTab extends CustomComponent {
	VerticalLayout mainLayout = new VerticalLayout();

	@Autowired
	private transient TaskService taskService;
	@Autowired
	private transient TaxTimeSheetLogService logService;

	private BeanItem<Task> taskItem;
	private Task task;
	private Form timeSheetForm;

	public FillUpTimeSheetTab() {
		setCaption("Add New Task");
		initContent();
	}

	private void initContent() {
		Panel panel = new Panel();
		task = new Task();
		timeSheetForm = new Form();

		taskItem = new BeanItem<Task>(task);
		timeSheetForm.setCaption("Create Task");
		timeSheetForm.setWriteThrough(false); // we want explicit 'apply'
		timeSheetForm.setInvalidCommitted(false); // no invalid values in
												// datamodel

		// FieldFactory for customizing the fields and adding validators
		timeSheetForm.setFormFieldFactory(new TimeSheetFieldsFactory());
		timeSheetForm.setItemDataSource(taskItem); // bind to POJO via BeanItem

		// Determines which properties are shown, and in which order:
		timeSheetForm.setVisibleItemProperties(Arrays.asList(new String[] {
				"company", "taskName", "period", "unit", "startDate", "description" }));
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);

		Button apply = new Button("Apply", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				try {
					timeSheetForm.commit();
					task = taskService.createTask(task);
					getWindow().showNotification("You task has been saved.");
					refresh();

					logService.addLog(ConstantResource.getCreateTask(), "");
				} catch (Exception e) {
					// Ignored, we'll let the Form handle the errors
					System.out.print(e);
				}
			}
		});
		buttons.addComponent(apply);
		timeSheetForm.getFooter().addComponent(buttons);
		timeSheetForm.getFooter().setMargin(false, false, true, true);

		panel.addComponent(timeSheetForm);
		mainLayout.addComponent(panel);
		this.setCompositionRoot(mainLayout);
	}
	
	public void refresh()
	{
		task = new Task();
		timeSheetForm.discard();
		taskItem = new BeanItem<Task>(task);
		timeSheetForm.setItemDataSource(taskItem);
		timeSheetForm.setVisibleItemProperties(Arrays.asList(new String[] {
				"company", "taskName", "period", "unit", "startDate", "description" }));
	}
}
